<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="previous" href="Dllist.html">
<link rel="next" href="Enum.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of class methods" rel=Appendix href="index_methods.html">
<link title="Index of classes" rel=Appendix href="index_classes.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Base64" rel="Chapter" href="Base64.html">
<link title="BitSet" rel="Chapter" href="BitSet.html">
<link title="Dllist" rel="Chapter" href="Dllist.html">
<link title="DynArray" rel="Chapter" href="DynArray.html">
<link title="Enum" rel="Chapter" href="Enum.html">
<link title="ExtArray" rel="Chapter" href="ExtArray.html">
<link title="ExtHashtbl" rel="Chapter" href="ExtHashtbl.html">
<link title="ExtLib" rel="Chapter" href="ExtLib.html">
<link title="ExtList" rel="Chapter" href="ExtList.html">
<link title="ExtString" rel="Chapter" href="ExtString.html">
<link title="Global" rel="Chapter" href="Global.html">
<link title="IO" rel="Chapter" href="IO.html">
<link title="OptParse" rel="Chapter" href="OptParse.html">
<link title="Option" rel="Chapter" href="Option.html">
<link title="PMap" rel="Chapter" href="PMap.html">
<link title="RefList" rel="Chapter" href="RefList.html">
<link title="Std" rel="Chapter" href="Std.html">
<link title="UChar" rel="Chapter" href="UChar.html">
<link title="UTF8" rel="Chapter" href="UTF8.html">
<link title="Unzip" rel="Chapter" href="Unzip.html"><link title="Array creation" rel="Section" href="#6_Arraycreation">
<link title="Array manipulation functions" rel="Section" href="#6_Arraymanipulationfunctions">
<link title="Array copy and conversion" rel="Section" href="#6_Arraycopyandconversion">
<link title="Array functional support" rel="Section" href="#6_Arrayfunctionalsupport">
<link title="Array resizers" rel="Section" href="#6_Arrayresizers">
<link title="Unsafe operations" rel="Section" href="#6_Unsafeoperations">
<title>DynArray</title>
</head>
<body>
<div class="navbar"><a class="pre" href="Dllist.html" title="Dllist">Previous</a>
&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;<a class="post" href="Enum.html" title="Enum">Next</a>
</div>
<h1>Module <a href="type_DynArray.html">DynArray</a></h1>
<pre><span class="keyword">module</span> DynArray: <code class="code">sig</code> <a href="DynArray.html">..</a> <code class="code">end</code></pre><div class="info">
Dynamic arrays.
<p>

   A dynamic array is equivalent to a OCaml array that will resize itself
   when elements are added or removed, except that floats are boxed and
   that no initialization element is required.<br>
</div>
<hr width="100%">
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">'a</code> t</span> </pre>

<pre><span id="EXCEPTIONInvalid_arg"><span class="keyword">exception</span> Invalid_arg</span> <span class="keyword">of</span> <code class="type">int * string * string</code></pre>
<div class="info">
When an operation on an array fails, <code class="code">Invalid_arg</code> is raised. The
	integer is the value that made the operation fail, the first string
	contains the function name that has been called and the second string
	contains the parameter name that made the operation fail.<br>
</div>
<br>
<h6 id="6_Arraycreation">Array creation</h6><br>
<pre><span id="VALcreate"><span class="keyword">val</span> create</span> : <code class="type">unit -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">create()</code> returns a new empty dynamic array.<br>
</div>
<pre><span id="VALmake"><span class="keyword">val</span> make</span> : <code class="type">int -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">make count</code> returns an array with some memory already allocated so
	up to <code class="code">count</code> elements can be stored into it without resizing.<br>
</div>
<pre><span id="VALinit"><span class="keyword">val</span> init</span> : <code class="type">int -> (int -> 'a) -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">init n f</code> returns an array of <code class="code">n</code> elements filled with values
	returned by <code class="code">f 0 , f 1, ... f (n-1)</code>.<br>
</div>
<br>
<h6 id="6_Arraymanipulationfunctions">Array manipulation functions</h6><br>
<pre><span id="VALempty"><span class="keyword">val</span> empty</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> bool</code></pre><div class="info">
Return true if the number of elements in the array is 0.<br>
</div>
<pre><span id="VALlength"><span class="keyword">val</span> length</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int</code></pre><div class="info">
Return the number of elements in the array.<br>
</div>
<pre><span id="VALget"><span class="keyword">val</span> get</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a</code></pre><div class="info">
<code class="code">get darr idx</code> gets the element in <code class="code">darr</code> at index <code class="code">idx</code>. If <code class="code">darr</code> has
	<code class="code">len</code> elements in it, then the valid indexes range from <code class="code">0</code> to <code class="code">len-1</code>.<br>
</div>
<pre><span id="VALlast"><span class="keyword">val</span> last</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a</code></pre><div class="info">
<code class="code">last darr</code> returns the last element of <code class="code">darr</code>.<br>
</div>
<pre><span id="VALset"><span class="keyword">val</span> set</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a -> unit</code></pre><div class="info">
<code class="code">set darr idx v</code> sets the element of <code class="code">darr</code> at index <code class="code">idx</code> to value
	<code class="code">v</code>.  The previous value is overwritten.<br>
</div>
<pre><span id="VALinsert"><span class="keyword">val</span> insert</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a -> unit</code></pre><div class="info">
<code class="code">insert darr idx v</code> inserts <code class="code">v</code> into <code class="code">darr</code> at index <code class="code">idx</code>.  All elements
	of <code class="code">darr</code> with an index greater than or equal to <code class="code">idx</code> have their
	index incremented (are moved up one place) to make room for the new
	element.<br>
</div>
<pre><span id="VALadd"><span class="keyword">val</span> add</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a -> unit</code></pre><div class="info">
<code class="code">add darr v</code> appends <code class="code">v</code> onto <code class="code">darr</code>.  <code class="code">v</code> becomes the new
	last element of <code class="code">darr</code>.<br>
</div>
<pre><span id="VALappend"><span class="keyword">val</span> append</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
<code class="code">append src dst</code> adds all elements of <code class="code">src</code> to the end of <code class="code">dst</code>.<br>
</div>
<pre><span id="VALdelete"><span class="keyword">val</span> delete</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> unit</code></pre><div class="info">
<code class="code">delete darr idx</code> deletes the element of <code class="code">darr</code> at <code class="code">idx</code>.  All elements
	with an index greater than <code class="code">idx</code> have their index decremented (are
	moved down one place) to fill in the hole.<br>
</div>
<pre><span id="VALdelete_last"><span class="keyword">val</span> delete_last</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
<code class="code">delete_last darr</code> deletes the last element of <code class="code">darr</code>. This is equivalent
	of doing <code class="code">delete darr ((length darr) - 1)</code>.<br>
</div>
<pre><span id="VALdelete_range"><span class="keyword">val</span> delete_range</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> int -> unit</code></pre><div class="info">
<code class="code">delete_range darr p len</code> deletes <code class="code">len</code> elements starting at index <code class="code">p</code>.
	All elements with an index greater than <code class="code">p+len</code> are moved to fill
	in the hole.<br>
</div>
<pre><span id="VALclear"><span class="keyword">val</span> clear</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
remove all elements from the array and resize it to 0.<br>
</div>
<pre><span id="VALblit"><span class="keyword">val</span> blit</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a <a href="DynArray.html#TYPEt">t</a> -> int -> int -> unit</code></pre><div class="info">
<code class="code">blit src srcidx dst dstidx len</code> copies <code class="code">len</code> elements from <code class="code">src</code>
	starting with index <code class="code">srcidx</code> to <code class="code">dst</code> starting at <code class="code">dstidx</code>.<br>
</div>
<pre><span id="VALcompact"><span class="keyword">val</span> compact</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
<code class="code">compact darr</code> ensures that the space allocated by the array is minimal.<br>
</div>
<br>
<h6 id="6_Arraycopyandconversion">Array copy and conversion</h6><br>
<pre><span id="VALto_list"><span class="keyword">val</span> to_list</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a list</code></pre><div class="info">
<code class="code">to_list darr</code> returns the elements of <code class="code">darr</code> in order as a list.<br>
</div>
<pre><span id="VALto_array"><span class="keyword">val</span> to_array</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a array</code></pre><div class="info">
<code class="code">to_array darr</code> returns the elements of <code class="code">darr</code> in order as an array.<br>
</div>
<pre><span id="VALenum"><span class="keyword">val</span> enum</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a <a href="Enum.html#TYPEt">Enum.t</a></code></pre><div class="info">
<code class="code">enum darr</code> returns the enumeration of <code class="code">darr</code> elements.<br>
</div>
<pre><span id="VALof_list"><span class="keyword">val</span> of_list</span> : <code class="type">'a list -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">of_list lst</code> returns a dynamic array with the elements of <code class="code">lst</code> in
	it in order.<br>
</div>
<pre><span id="VALof_array"><span class="keyword">val</span> of_array</span> : <code class="type">'a array -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">of_array arr</code> returns an array with the elements of <code class="code">arr</code> in it
	in order.<br>
</div>
<pre><span id="VALof_enum"><span class="keyword">val</span> of_enum</span> : <code class="type">'a <a href="Enum.html#TYPEt">Enum.t</a> -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">of_enum e</code> returns an array that holds, in order, the elements of <code class="code">e</code>.<br>
</div>
<pre><span id="VALcopy"><span class="keyword">val</span> copy</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">copy src</code> returns a fresh copy of <code class="code">src</code>, such that no modification of
	<code class="code">src</code> affects the copy, or vice versa (all new memory is allocated for
	the copy).<br>
</div>
<pre><span id="VALsub"><span class="keyword">val</span> sub</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> int -> 'a <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">sub darr start len</code> returns an array holding the subset of <code class="code">len</code>
	elements from <code class="code">darr</code> starting with the element at index <code class="code">idx</code>.<br>
</div>
<br>
<h6 id="6_Arrayfunctionalsupport">Array functional support</h6><br>
<pre><span id="VALiter"><span class="keyword">val</span> iter</span> : <code class="type">('a -> unit) -> 'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
<code class="code">iter f darr</code> calls the function <code class="code">f</code> on every element of <code class="code">darr</code>.  It
	is equivalent to <code class="code">for i = 0 to length darr - 1 do f (get darr i) done;</code><br>
</div>
<pre><span id="VALiteri"><span class="keyword">val</span> iteri</span> : <code class="type">(int -> 'a -> unit) -> 'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><div class="info">
<code class="code">iteri f darr</code> calls the function <code class="code">f</code> on every element of <code class="code">darr</code>.  It
	is equivalent to <code class="code">for i = 0 to length darr - 1 do f i (get darr i) done;</code><br>
</div>
<pre><span id="VALmap"><span class="keyword">val</span> map</span> : <code class="type">('a -> 'b) -> 'a <a href="DynArray.html#TYPEt">t</a> -> 'b <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">map f darr</code> applies the function <code class="code">f</code> to every element of <code class="code">darr</code>
	and creates a dynamic array from the results - similar to <code class="code">List.map</code> or
	<code class="code">Array.map</code>.<br>
</div>
<pre><span id="VALmapi"><span class="keyword">val</span> mapi</span> : <code class="type">(int -> 'a -> 'b) -> 'a <a href="DynArray.html#TYPEt">t</a> -> 'b <a href="DynArray.html#TYPEt">t</a></code></pre><div class="info">
<code class="code">mapi f darr</code> applies the function <code class="code">f</code> to every element of <code class="code">darr</code>
	and creates a dynamic array from the results - similar to <code class="code">List.mapi</code> or
	<code class="code">Array.mapi</code>.<br>
</div>
<pre><span id="VALfold_left"><span class="keyword">val</span> fold_left</span> : <code class="type">('a -> 'b -> 'a) -> 'a -> 'b <a href="DynArray.html#TYPEt">t</a> -> 'a</code></pre><div class="info">
<code class="code">fold_left f x darr</code> computes
	<code class="code">f ( ... ( f ( f (get darr 0) x) (get darr 1) ) ... ) (get darr n-1)</code>,
	similar to <code class="code">Array.fold_left</code> or <code class="code">List.fold_left</code>.<br>
</div>
<pre><span id="VALfold_right"><span class="keyword">val</span> fold_right</span> : <code class="type">('a -> 'b -> 'b) -> 'a <a href="DynArray.html#TYPEt">t</a> -> 'b -> 'b</code></pre><div class="info">
<code class="code">fold_right f darr x</code> computes
	<code class="code"> f (get darr 0) (f (get darr 1) ( ... ( f (get darr n-1) x ) ... ) ) </code>
	similar to <code class="code">Array.fold_right</code> or <code class="code">List.fold_right</code>.<br>
</div>
<pre><span id="VALindex_of"><span class="keyword">val</span> index_of</span> : <code class="type">('a -> bool) -> 'a <a href="DynArray.html#TYPEt">t</a> -> int</code></pre><div class="info">
<code class="code">index_of f darr</code> returns the index of the first element <code class="code">x</code> in darr such
	as <code class="code">f x</code> returns <code class="code">true</code> or raise <code class="code">Not_found</code> if not found.<br>
</div>
<pre><span id="VALfilter"><span class="keyword">val</span> filter</span> : <code class="type">('a -> bool) -> 'a <a href="DynArray.html#TYPEt">t</a> -> unit</code></pre><br>
<h6 id="6_Arrayresizers">Array resizers</h6><br>
<pre><span id="TYPEresizer_t"><span class="keyword">type</span> <code class="type"></code>resizer_t</span> = <code class="type">currslots:int -> oldlength:int -> newlength:int -> int</code> </pre>
<div class="info">
The type of a resizer function.
<p>

	Resizer functions are called whenever elements are added to
	or removed from the dynamic array to determine what the current number of
	storage spaces in the array should be.  The three named arguments
	passed to a resizer are the current number of storage spaces in
	the array, the length of the array before the elements are
	added or removed, and the length the array will be after the
	elements are added or removed.  If elements are being added, newlength
	will be larger than oldlength, if elements are being removed,
	newlength will be smaller than oldlength. If the resizer function
	returns exactly oldlength, the size of the array is only changed when
	adding an element while there is not enough space for it.
<p>

	By default, all dynamic arrays are created with the <code class="code">default_resizer</code>.
	When a dynamic array is created from another dynamic array (using <code class="code">copy</code>,
	<code class="code">map</code> , etc. ) the resizer of the copy will be the same as the original
	dynamic array resizer. To change the resizer, use the <code class="code">set_resizer</code>
	function.<br>
</div>

<pre><span id="VALset_resizer"><span class="keyword">val</span> set_resizer</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> <a href="DynArray.html#TYPEresizer_t">resizer_t</a> -> unit</code></pre><div class="info">
Change the resizer for this array.<br>
</div>
<pre><span id="VALget_resizer"><span class="keyword">val</span> get_resizer</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> <a href="DynArray.html#TYPEresizer_t">resizer_t</a></code></pre><div class="info">
Get the current resizer function for a given array<br>
</div>
<pre><span id="VALdefault_resizer"><span class="keyword">val</span> default_resizer</span> : <code class="type"><a href="DynArray.html#TYPEresizer_t">resizer_t</a></code></pre><div class="info">
The default resizer function the library is using - in this version
	of DynArray, this is the <code class="code">exponential_resizer</code> but should change in
	next versions.<br>
</div>
<pre><span id="VALexponential_resizer"><span class="keyword">val</span> exponential_resizer</span> : <code class="type"><a href="DynArray.html#TYPEresizer_t">resizer_t</a></code></pre><div class="info">
The exponential resizer- The default resizer except when the resizer
	is being copied from some other darray.
<p>

	<code class="code">exponential_resizer</code> works by doubling or halving the number of
	slots until they "fit".  If the number of slots is less than the
	new length, the number of slots is doubled until it is greater
	than the new length (or Sys.max_array_size is reached).
<p>

	If the number of slots is more than four times the new length,
	the number of slots is halved until it is less than four times the
	new length.
<p>

	Allowing darrays to fall below 25% utilization before shrinking them
	prevents "thrashing".  Consider the case where the caller is constantly
	adding a few elements, and then removing a few elements, causing
	the length to constantly cross above and below a power of two.
	Shrinking the array when it falls below 50% would causing the
	underlying array to be constantly allocated and deallocated.
	A few elements would be added, causing the array to be reallocated
	and have a usage of just above 50%.  Then a few elements would be
	remove, and the array would fall below 50% utilization and be
	reallocated yet again.  The bulk of the array, untouched, would be
	copied and copied again.  By setting the threshold at 25% instead,
	such "thrashing" only occurs with wild swings- adding and removing
	huge numbers of elements (more than half of the elements in the array).
<p>

	<code class="code">exponential_resizer</code> is a good performing resizer for most
	applications.  A list allocates 2 words for every element, while an
	array (with large numbers of elements) allocates only 1 word per
	element (ignoring unboxed floats).  On insert, <code class="code">exponential_resizer</code>
	keeps the amount of wasted "extra" array elements below 50%, meaning
	that less than 2 words per element are used.  Even on removals
	where the amount of wasted space is allowed to rise to 75%, that
	only means that darray is using 4 words per element.  This is
	generally not a significant overhead.
<p>

	Furthermore, <code class="code">exponential_resizer</code> minimizes the number of copies
	needed- appending n elements into an empty darray with initial size
	0 requires between n and 2n elements of the array be copied- O(n)
	work, or O(1) work per element (on average).  A similar argument
	can be made that deletes from the end of the array are O(1) as
	well (obviously deletes from anywhere else are O(n) work- you
	have to move the n or so elements above the deleted element down).<br>
</div>
<pre><span id="VALstep_resizer"><span class="keyword">val</span> step_resizer</span> : <code class="type">int -> <a href="DynArray.html#TYPEresizer_t">resizer_t</a></code></pre><div class="info">
The stepwise resizer- another example of a resizer function, this
	time of a parameterized resizer.
<p>

	The resizer returned by <code class="code">step_resizer step</code> returns the smallest
	multiple of <code class="code">step</code> larger than <code class="code">newlength</code> if <code class="code">currslots</code> is less
	then <code class="code">newlength</code>-<code class="code">step</code> or greater than <code class="code">newlength</code>.
<p>

	For example, to make an darray with a step of 10, a length
	of len, and a null of null, you would do:
	<code class="code">make</code> ~resizer:(<code class="code">step_resizer</code> 10) len null<br>
</div>
<pre><span id="VALconservative_exponential_resizer"><span class="keyword">val</span> conservative_exponential_resizer</span> : <code class="type"><a href="DynArray.html#TYPEresizer_t">resizer_t</a></code></pre><div class="info">
<code class="code">conservative_exponential_resizer</code> is an example resizer function
	which uses the oldlength parameter.  It only shrinks the array
	on inserts- no deletes shrink the array, only inserts.  It does
	this by comparing the oldlength and newlength parameters.  Other
	than that, it acts like <code class="code">exponential_resizer</code>.<br>
</div>
<br>
<h6 id="6_Unsafeoperations">Unsafe operations</h6> *<br>
<pre><span id="VALunsafe_get"><span class="keyword">val</span> unsafe_get</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a</code></pre><pre><span id="VALunsafe_set"><span class="keyword">val</span> unsafe_set</span> : <code class="type">'a <a href="DynArray.html#TYPEt">t</a> -> int -> 'a -> unit</code></pre></body></html>